<?php

/**
 * This is the model class for table "tb_Usuarios".
 *
 * The followings are the available columns in table 'tb_Usuarios':
 * @property string $login
 * @property string $password
 * @property string $nombre
 * @property string $apellido
 * @property integer $estado
 * @property integer $tb_tipousuarios_idUserType
 */
class TbCambioContra extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return TbCambioContra the static model class
	 */
	public $usuario;
	public $contraActual;
	public $contraNueva;
	public $contraConfirme;
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'tb_Usuarios';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('contraActual, contraNueva, contraConfirme', 'required'),
			array('estado, tb_tipousuarios_idUserType', 'numerical', 'integerOnly'=>true),
			array('login, nombre, apellido', 'length', 'max'=>50),
			array('contraActual, contraNueva, contraConfirme', 'length', 'max'=>20),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'login' => 'Login',
			'password' => 'Password',
			'nombre' => 'Nombre',
			'apellido' => 'Apellido',
			'estado' => 'Estado',
			'tb_tipousuarios_idUserType' => 'Tb Tipousuarios Id User Type',
			'contraActual'=>'Contrase&ntilde;a Actual',
			'contraNueva'=>'Nueva Contrase&ntilde;a',
			'contraConfirme'=>'Confirme Contrase&ntilde;a',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('login',$this->login,true);
		$criteria->compare('password',$this->password,true);
		$criteria->compare('nombre',$this->nombre,true);
		$criteria->compare('apellido',$this->apellido,true);
		$criteria->compare('estado',$this->estado);
		$criteria->compare('tb_tipousuarios_idUserType',$this->tb_tipousuarios_idUserType);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	public function cargarUsuario(){
		$usu = TbUsuarios::model()->findByPk($this->usuario);
		$error = 0;
		if (strlen($this->contraActual)<5 || strlen($this->contraConfirme)<5 || strlen($this->contraNueva)<5)
			return 4;
		if ($usu===null){
			return 1;	
		}
		if ($this->contraActual !== $usu->password){
			return 2;
		}
		if ($this->contraNueva!==$this->contraConfirme){
			return 3;
		}
		$usu->password = $this->contraNueva;
		$usu->save();
		return 0;
	}
}